package org.msd.controller;
import org.apache.ibatis.annotations.Param;
import org.msd.model.Commodity;
import org.msd.model.Reserve;
import org.msd.service.iml.ReServeService;
import org.msd.service.iml.ZoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/status")
public class Scheduled {
    @Autowired
    private ZoomService zoomService;
    @Autowired
    private ReServeService reServeService;
    /*
    预定功能
     */

    /*
   前端传递到后端商品的id
     */
    @RequestMapping(value = "selectById",method = RequestMethod.GET)
    public String Reservationpage(@RequestParam("commodityid")int commodityid, Model model){
        Commodity commodity = zoomService.GETProductid(commodityid);
          if (commodity!=null){
              model.addAttribute("hotelreservation", commodity);
              return "Advance";//Advanc就是预定页面
          }else {
              return "Test";
          }

        }

    /*
    插入数据
     */
    @RequestMapping("scheduled")
    @ResponseBody
    public double Scheduled(Reserve reserve) {



        //插入成功跳转到Bookingsuccessful页面

        int reserve1 =reServeService.insertReserve(reserve);

        if (reserve1 == 1) {
            int Alreadybooked = zoomService.Changestatus(reserve.getReservecommodity(), "已预定");

            int    date= (int) (reserve.getStartingtime().getTime()-reserve.getTimeofdepartrueDate().getTime());//时间相加减

             Commodity  commodity=zoomService.Checkrent(reserve.getReservecommodity());//查询价格、

               int  data1=date/60/60/60;

              double money=commodity.getPrice()*data1;//计算价格
/*
              根据前端传递的id改预定中
               */
            if (Alreadybooked == 1) {

                return money;
            }

        }
               return 0;


    }
    /*
    查询个人信息
     */
    @RequestMapping("queryindividuals")
    public String  Query(Model model, @Param("phone") String phone){
        List<Reserve> reserve=reServeService.Querybyid(phone);//根据查询查询所有结果
        if (reserve!=null){
            model.addAttribute("listreserve",reserve);
            return "Test";
        }else{
            model.addAttribute("listreserve1","输出不对");
        }
       return "Test";
    }
    @RequestMapping("checkout")
    @ResponseBody
    public  String    CheckOUT(@RequestParam("reservecommodity") int  reservecommodity){
        int commodityid=reservecommodity;
        int  p=zoomService.Changestatus(commodityid,"无人");
        if (p==1){
            return "退房成功";
        }else {

        }
        return null;
    }

}
